<template>
    <div class="edit-popup">
        <popup
            ref="popupRef"
            :title="popupTitle"
            :async="true"
            width="550px"
            @confirm="handleSubmit"
            @close="handleClose"
        >
            <el-form
                class="ls-form"
                ref="formRef"
                :rules="rules"
                :model="formData"
                label-width="84px"
            >
                <el-form-item label="视频名称" prop="title">
                    <el-input v-model="formData.title" placeholder="请输入视频名称" />
                </el-form-item>
                <el-form-item label="视频封面" prop="video">
                    <!-- <el-upload
                        class="avatar-uploader"
                        :action="upLoadUrl"
                        :show-file-list="false"
                        :on-success="handleAvatarSuccess"
                        :before-upload="beforeAvatarUpload"
                        accept="video/*"
                    >
                    <video class="video" v-if="formData.cover" :src="formData.cover" initial-time="0" autoplay="false" loop="false" muted="false" direction="0" 
                        bindplay="" bindpause="" bindended="" bindtimeupdate="" bindwaiting="" binderror="">
                        
                    </video>
                        <el-icon v-else class="avatar-uploader-icon"><Plus /></el-icon>
                    </el-upload> -->
                    <VideoUploader @success="emitSuccess" />
                </el-form-item>
                
                <!-- <el-form-item label="视频描述" prop="desc">
                    <el-input
                        v-model="formData.desc"
                        type="textarea"
                        :autosize="{ minRows: 4, maxRows: 6 }"
                        clearable
                        maxlength="200"
                        show-word-limit
                    />
                </el-form-item> -->
            </el-form>
        </popup>
    </div>
</template>
<script lang="ts" setup>
import type { FormInstance, UploadProps } from 'element-plus'
import { apiVideoAdd, apiVideoEdit } from '@/api/video';
import Popup from '@/components/popup/index.vue'
import { ref, reactive, computed, shallowRef, defineProps, toRaw } from 'vue'
import feedback from '@/utils/feedback'
import VideoUploader from '@/components/upload/VideoUploader.vue'; // 假设你的组件路径是这个
const emit = defineEmits(['success', 'close'])
const formRef = shallowRef<FormInstance>()
const popupRef = shallowRef<InstanceType<typeof Popup>>()
const mode = ref('add')
const popupTitle = computed(() => {
    return mode.value == 'edit' ? '编辑视频' : '新增视频'
})

const albumId = ref()

const formData = reactive({
    mu_id: '',
    title: '',
    video: '',
    // desc: '',
})

const rules = {
    title: [
        {
            required: true,
            message: '请输入视频名称',
            trigger: ['blur']
        }
    ],
    video: [
        {
            required: true,
            message: '请上传视频封面',
            trigger: ['blur']
        }
    ]
}

const handleSubmit = async () => {
    await formRef.value?.validate()
    mode.value == 'edit' ? await apiVideoEdit({...formData, id: albumId.value}) : await apiVideoAdd(formData)
    popupRef.value?.close()
    emit('success')
}

const handleClose = () => {
    emit('close')
}

const open = (type = 'add') => {
    mode.value = type
    popupRef.value?.open()
}

const setFormData = (data: Record<any, any>) => {
    for (const key in formData) {
        if (data[key] != null && data[key] != undefined) {
            //@ts-ignore
            formData[key] = data[key]
        }
    }
    if(data.id){
        albumId.value = data.id
    }
}

const emitSuccess = (url: string) => {
    console.log('url====================',url)
    formData.video = url
}

defineExpose({
    open,
    setFormData
})
</script>
<style scoped>
    .avatar-uploader{
        border: 1px dashed #cdd0d6;
        border-radius: 6px;
        overflow: hidden;
        width: 100px;
        height: 100px;
        box-sizing: border-box;
    }
    .avatar-uploader .avatar {
        width: 100px;
        height: 100px;
        display: block;
    }

    .avatar-uploader .el-upload {
        border: 1px dashed var(--el-border-color);
        border-radius: 6px;
        cursor: pointer;
        position: relative;
        overflow: hidden;
        transition: var(--el-transition-duration-fast);
    }

    .avatar-uploader .el-upload:hover {
        border-color: var(--el-color-primary);
    }

    .el-icon.avatar-uploader-icon {
        font-size: 28px;
        color: #8c939d;
        width: 100px;
        height: 100px;
        text-align: center;
    }
</style>
